//
//  JPSuspensionFunctionView.m
//  JPSuspensionViewDemo
//
//  Created by Carpenter on 2018/2/12.
//  Copyright © 2018年 carpenter. All rights reserved.
//

#import "JPSuspensionFunctionView.h"
#import <Masonry/Masonry.h>
#import <pop/POP.h>
#import "JPSuspensionConfigure.h"
#import "JPSuspensionFunctionBaseContentView.h"


#define JPSF_SCREEN_SIZE  ([UIScreen mainScreen].bounds.size)

@interface JPSuspensionFunctionView()


/**
 代理响应者
 */
@property (nonatomic ,weak) id<JPSuspensionFunctionViewDelegate> myDelegate;

/**
 父视图（承载视图）
 */
@property (nonatomic ,weak) UIView * superView;

/**
 展示视图
 */
@property (nonatomic ,strong)JPSuspensionFunctionBaseContentView * contentView;


@end

@implementation JPSuspensionFunctionView

+(instancetype)jp_sharedWithContent:(JPSuspensionFunctionBaseContentView *)contentView delegate:(id<JPSuspensionFunctionViewDelegate>)delegate showInView:(UIView *)view{
    
    JPSuspensionFunctionView * function = [[JPSuspensionFunctionView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    function.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.01];
    
    function.myDelegate = delegate;
    
    function.contentView = contentView;
    
    [function addSubview:contentView];
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.width.mas_equalTo(JPSF_SCREEN_SIZE.width * contentView.configure.widthRatio);
    }];
    
    __weak typeof(function)weakFunction = function;
    
    contentView.didFinishShowingHandleBlock = ^(id data) {
        
        __strong typeof(weakFunction)strongFunction = weakFunction;
        
        if (strongFunction.myDelegate && [strongFunction.myDelegate respondsToSelector:@selector(suspensionFunctionView:didFinishShowingHandleData:)]) {
            [strongFunction.myDelegate suspensionFunctionView:strongFunction.contentView didFinishShowingHandleData:data];
        }
        [strongFunction jp_hide];
    };
    
    contentView.didCancelButtonBlock = ^{
        
        __strong typeof(weakFunction)strongFunction = weakFunction;
        
        if (strongFunction.myDelegate && [strongFunction.myDelegate respondsToSelector:@selector(suspensionFunctionViewDidCancel:)]) {
            [strongFunction.myDelegate suspensionFunctionViewDidCancel:strongFunction.contentView];
        }
        [strongFunction jp_hide];
    };
    
    [function jp_showInView:view];
    
    return function;
}
//MARK: - 展示视图
-(void)jp_showInView:(UIView *)superView{
    
    if (superView == nil) {
        superView = [UIApplication sharedApplication].delegate.window;
    }
    
    [superView addSubview:self];
    [superView bringSubviewToFront:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    POPBasicAnimation * basic = [POPBasicAnimation animationWithPropertyNamed:kPOPViewBackgroundColor];
    basic.duration =self.contentView.configure.animatedDurationTime;
    basic.toValue = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    [self pop_addAnimation:basic forKey:@"ColorAlpha"];
    
    POPSpringAnimation * animate = [POPSpringAnimation animationWithPropertyNamed:kPOPViewScaleXY];
    animate.fromValue = [NSValue valueWithCGPoint:CGPointMake(0.6, 0.6)];
    animate.toValue = [NSValue valueWithCGPoint:CGPointMake(1.0, 1.0)];
    animate.springSpeed = 15;
    animate.springBounciness = 10;
    [_contentView pop_addAnimation:animate forKey:@"ScaleBounds"];
    
}
//MARK: - 移除视图
-(void)jp_hide{
    
    POPBasicAnimation * removeBasicAnimate = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    removeBasicAnimate.duration = self.contentView.configure.animatedDurationTime;
    removeBasicAnimate.toValue = @(0.01);
    [self pop_addAnimation:removeBasicAnimate forKey:@"remove"];
    removeBasicAnimate.completionBlock = ^(POPAnimation *anim, BOOL finished) {
        [anim pop_removeAnimationForKey:@"remove"];
        [self removeFromSuperview];
    };
    
}
@end
